package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.VerifyCode;
import com.sanjishu.enterlab.electricity.sale.mapper.VerifyCodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class VerifyCodeService {

    @Autowired
    private VerifyCodeMapper verifyCodeMapper;

    public void createVerifyCode(String phone, String code) {
        VerifyCode verifyCode = new VerifyCode();
        verifyCode.setCellphone(phone);
        verifyCode.setCode(code);
        verifyCode.setStatus(VerifyCode.STATUS_NORMAL);
        verifyCode.setTypes(VerifyCode.TYPE_1);
        verifyCode.setCreateAt(new Date());
        verifyCodeMapper.insert(verifyCode);
    }

    public Response checkCode(String phone, String secCode) {
        VerifyCode verifyCode = verifyCodeMapper.checkCode(phone, secCode);
        if (verifyCode != null) {
            verifyCode.setStatus(VerifyCode.STATUS_USED);
            verifyCode.setUpdateAt(new Date());
            verifyCodeMapper.updateByPrimaryKey(verifyCode);
            return Response.buildSuccess("验证通过");
        } else {
            return Response.getError("验证码错误，请重新尝试。");
        }
    }
}
